/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
*/
package radio_GUI;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.*;

/**
 *
 * @author elena
 */
public class EditMusicGroupAlbum_1 extends javax.swing.JFrame {

    Album a;
    Object[][] data;
    ArrayList <Song> songs;
    ArrayList<Musicgroup> musicgroups;
    ArrayList <String> types;
    ArrayList<Musicproductioncompany> musicproductions;
    private EntityManager em; //P.A. o entity manger tis basis
    PinakasAlbumMusicGroup pamg;
    boolean isNew;
    /**
     * Creates new form editMusicGroupAlbum
     */
    public EditMusicGroupAlbum_1(Album a, PinakasAlbumMusicGroup pamg) {

        this.em=DBManager.getEm();
        initComponents();
        
        this.pamg=pamg;
        this.a=a;
        songs=new ArrayList();
        musicgroups=new ArrayList();
        musicproductions=new ArrayList();
        
        jComboBox2.removeAllItems();//αφαίρεση τιμών από ανααδυώμενη λίστα
        for (int i=1; i<32; i++){
            jComboBox2.addItem(i);
        }
        jComboBox3.removeAllItems();//αφαίρεση τιμών από ανααδυώμενη λίστα
        for (int i=1; i<13; i++){
            jComboBox3.addItem(i);
        }
        jComboBox4.removeAllItems();//αφαίρεση τιμών από ανααδυώμενη λίστα
        for (int i=1900; i<2016; i++){
            jComboBox4.addItem(i);
        }
        jComboBox6.removeAllItems();//αφαίρεση τιμών από ανααδυώμενη λίστα
        for (int i=1; i<10; i++){
            jComboBox6.addItem(i);
        }
        
        jComboBox7.removeAllItems();//αφαίρεση τιμών από ανααδυώμενη λίστα
        Query q = em.createQuery("SELECT mp FROM Musicproductioncompany mp ", Musicproductioncompany.class);//ανάγνωση καταστημάτων από βάση
        List result = q.getResultList();
        
        if (!result.isEmpty()) { // αν επιστραφουν αποτελέσματα
            Iterator itr=result.iterator();//δεικτης στα καταστήματα
            //διάτρεξη καταστημάτων
            while (itr.hasNext()){
                Musicproductioncompany mp = (Musicproductioncompany)itr.next();
                //jComboBox5.addItem(m);//φόρτωση καταστημάτων στν αναδόμενη λίστα
                musicproductions.add(mp);
                jComboBox7.addItem(mp);
            }
        }
        jComboBox1.removeAllItems();//αφαίρεση τιμών από ανααδυώμενη λίστα
        q = em.createQuery("SELECT mg FROM Musicgroup mg ", Musicgroup.class);//ανάγνωση καταστημάτων από βάση
        result = q.getResultList();
        
        if (!result.isEmpty()) { // αν επιστραφουν αποτελέσματα
            Iterator itr=result.iterator();//δεικτης στα καταστήματα
            //διάτρεξη καταστημάτων
            while (itr.hasNext()){
                Musicgroup mg = (Musicgroup)itr.next();
                //jComboBox5.addItem(m);//φόρτωση καταστημάτων στν αναδόμενη λίστα
                musicgroups.add(mg);
                jComboBox1.addItem(mg);
            }
        }
        types=new ArrayList();
        jComboBox5.removeAllItems();//αφαίρεση τιμών από ανααδυώμενη λίστα
        types.add("LP");
        types.add("EP");
        types.add("CS");
        Iterator itr=types.iterator();//δεικτης στα καταστήματα
        //διάτρεξη καταστημάτων
        while (itr.hasNext()){
            String s = (String)itr.next();
            //jComboBox5.addItem(m);//φόρτωση καταστημάτων στν αναδόμενη λίστα
            jComboBox5.addItem(s);
        }
        if (a!=null){
            isNew=false;
            data = new Object[a.getSongList().size()][3];
            jTextField1.setText(a.getTitle());
            jComboBox1.setSelectedItem(a.getMpcId());
            jComboBox2.setSelectedItem(a.getReleasedate().getDate());
            jComboBox3.setSelectedItem(a.getReleasedate().getMonth()+1);
            jComboBox4.setSelectedItem(a.getReleasedate().getYear()+1900);
            jComboBox5.setSelectedItem(a.getType());
            jComboBox6.setSelectedItem(a.getDisknumber());
            //jComboBox7.setSelectedItem(a.getAlbumMgList().get(0));
            jComboBox7.setSelectedItem(a.getMpcId());
            this.songs=new ArrayList();//(ArrayList)m.getMusicgroupArtistCollection();
            
            itr=a.getSongList().iterator();
            int tableCounter=0;
            while (itr.hasNext()){
                Song s1=(Song)itr.next();
                this.songs.add(s1);
                
                data[tableCounter][0]=s1.getTitle();
                data[tableCounter][1]=s1.getTracknr();
                data[tableCounter][2]=s1.getDuration();
                tableCounter++;
            }
        }
        else{
            isNew=true;
            data = new Object[0][3];
            this.a=new Album();
            this.a.setSongList(new ArrayList());
        }

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
             data,
             new String [] {
                 "Τίτλος", "Αριθμός", "Διάρκεια",
             }
         ));
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jComboBox5 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jComboBox6 = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jComboBox7 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 153, 0));
        jLabel1.setText("Άλμπουμ Συγκροτήματος");

        jLabel2.setText("Τίτλος:");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Εταιρεία:");

        jLabel4.setText("Συγκρότημα:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel5.setText("Ημερομηνία:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel6.setText("Τύπος:");

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel7.setText("Αρ. Δίσκων:");

        jComboBox6.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 153, 0));
        jLabel8.setText("Τραγούδια");

        jLabel9.setText("Τίτλος:");

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jLabel10.setText("Διάρκεια:");

        jLabel11.setText("Αριθμός:");

        jTextField5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField5ActionPerformed(evt);
            }
        });

        jButton1.setText("Προσθήκη");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText("Αποθήκευση");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Ακύρωση");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Διαγραφή");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jComboBox7.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(119, 119, 119)
                            .addComponent(jLabel1))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel5)
                            .addGap(18, 18, 18)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel8)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel9)
                            .addGap(26, 26, 26)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel10)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel11)
                            .addGap(18, 18, 18)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                            .addComponent(jButton4))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel7)
                                    .addGap(18, 18, 18)
                                    .addComponent(jComboBox6, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel4))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(19, 19, 19)
                                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(18, 18, 18)
                                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGap(17, 17, 17)
                                    .addComponent(jLabel6)
                                    .addGap(18, 18, 18)
                                    .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jComboBox7, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(41, 41, 41)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(jButton2)
                        .addGap(55, 55, 55)
                        .addComponent(jButton3)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jComboBox6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel8)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:        
        this.dispose();//kleisimo formas
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:

        if (!DBManager.getEm().getTransaction().isActive()){
            DBManager.getEm().getTransaction().begin();
        }
        try{
            Query q = DBManager.getEm().createQuery("SELECT a FROM Album a WHERE a.albumId="+a.getAlbumId(), Album.class);

            List result = q.getResultList();//λίστα αποτελεσμάτων
            if (!result.isEmpty()) {
                Iterator itr=result.iterator();
                while (itr.hasNext()){//διάτρεξη καλαθιού
                    this.a=(Album)itr.next();
                }
                //this.isNew=false;
                System.out.println(a.getSongList().size()+"-"+songs.size()+"-"+this.isNew);
                this.a.getSongList().isEmpty();
                this.a.setSongList(songs);
                
            }
            
            //q = DBManager.getEm().createQuery("DELETE FROM AlbumMg amg WHERE amg.albumId="+this.a.getAlbumId(), model.AlbumMg.class);
            //System.out.println("DELETE FROM AlbumMg amg WHERE amg.albumId="+this.a.getAlbumId());
            //q.executeUpdate();
                    
            this.a.setTitle(jTextField1.getText());
            //ArrayList mgrousp=new ArrayList();
            //mgrousp.add((Musicgroup)jComboBox1.getSelectedItem());
            System.out.println("aIDis"+this.a.getAlbumId());
            if (this.a.getAlbumId()==null){
                this.a.setAlbumMgList(new ArrayList());
            }
            if (this.a.getAlbumMgList().size()==0){
                model.AlbumMg amg=new model.AlbumMg();
                amg.setAlbumId(this.a);
                amg.setMusicgroupId((Musicgroup)jComboBox1.getSelectedItem());
                ArrayList albmgList=new ArrayList();

                albmgList.add(amg);
                this.a.setAlbumMgList(albmgList);
            }
            //System.out.println(mgrousp.get(0));
            //this.a.setAlbumMgList(mgrousp);
            this.a.setType((String)jComboBox5.getSelectedItem());
            this.a.setDisknumber((int)jComboBox6.getSelectedItem());
            this.a.setMpcId((Musicproductioncompany)jComboBox7.getSelectedItem());
            Date d = new Date();

            d.setDate((int) jComboBox2.getSelectedItem());
            d.setMonth((int) jComboBox3.getSelectedItem()-1);
            d.setYear((int) jComboBox4.getSelectedItem()-1900);
            this.a.setReleasedate(d);
            //ενημέρωση songlist
           // this.a.setSongList(null);
            //this.a.setSongList(songs);
            this.a.getSongList().isEmpty();
            //έλεγχος για συμπλήρωση υποχρεωτικών πεδίων
            if (!this.a.getTitle().equals("") && a.getSongList().size()>=1){
                if (!isNew){
                    this.em.merge(this.a);
                    this.em.flush();
                }else{
                    this.em.persist(this.a);//προσθήκη στη transaction με τη βάση
                }
                this.em.getTransaction().commit();//πραγματοποίηση αλλαγών στη βάση
                this.pamg.dispose();
                PinakasAlbumMusicGroup pamg2=new PinakasAlbumMusicGroup();
                pamg2.setVisible(true);
                this.dispose();//kleisimo formas
            }
            else{
                JOptionPane.showMessageDialog(null, "Συμπληρώστε ονομα και δώστε τουλάχιστον 2 τραγούδια", "Λάθος", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBManager.getEm().getTransaction().rollback();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        try{
            int duration=Integer.parseInt(jTextField4.getText());
            int trackNum=Integer.parseInt(jTextField5.getText());
            String name=jTextField3.getText();
            if (duration>0 && trackNum>0 && !name.equals("")){
                Song song=new Song();
                song.setTitle(name);
                song.setDuration(duration);
                song.setTracknr(trackNum);
                Iterator itr=songs.iterator();
                boolean found=false;
                while (itr.hasNext()){
                    Song s1=(Song)itr.next();
                    if (s1.getTitle().equals(song.getTitle()) && s1.getDuration()==song.getDuration()){
                        found=true;
                        break;
                    }
                }

                if (!found){
                    song.setAlbumId(a);
                    songs.add(song);
                    a.getSongList().add(song);

                    
                    data = new Object[a.getSongList().size()][3];
                    itr=a.getSongList().iterator();
                    int tableCounter=0;
                    this.songs.clear();//παρακτω το ξαναγεμιζουμε add(s1)
                    while (itr.hasNext()){
                        Song s1=(Song)itr.next();
                        this.songs.add(s1);
                        System.out.println(s1.getTitle());

                        data[tableCounter][0]=s1.getTitle();
                        data[tableCounter][1]=s1.getTracknr();
                        data[tableCounter][2]=s1.getDuration();
                        tableCounter++;
                    }

                    jTable1.setModel(new javax.swing.table.DefaultTableModel(
                        data,
                        new String [] {
                            "Τίτλος", "Αριθμός", "Διάρκεια",
                        }
                    ));
                    jTextField4.setText("");
                    jTextField5.setText("");
                    jTextField3.setText("");
                    
                }
                else{
                    JOptionPane.showMessageDialog(null, "Το τραγούδι υπάρχει ήδη", "Λάθος", JOptionPane.ERROR_MESSAGE);
                }
            }
            
            else{
                JOptionPane.showMessageDialog(null, "Συμπληρώστε όλα τα πεδία για το τραγούδι", "Λάθος", JOptionPane.ERROR_MESSAGE);
            }

        } catch (Exception e) {
            e.printStackTrace();
            DBManager.getEm().getTransaction().rollback();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        int num=jTable1.getSelectedRow();
        if (num>=0){//αν επέλεξα κάτι από τα καλλιτέχνη
            Song song = songs.get(num);
            songs.remove(song);
            a.getSongList().remove(song);
            if (!DBManager.getEm().getTransaction().isActive()){
                DBManager.getEm().getTransaction().begin();
            }
            //Query q = DBManager.getEm().createQuery("DELETE FROM PlaylistSong ps WHERE ps.songId.songId="+song.getSongId(), PlaylistSong.class);
            //q.executeUpdate();
            Query q = DBManager.getEm().createQuery("DELETE FROM Song s WHERE s.songId="+song.getSongId(), Song.class);
            q.executeUpdate();
            DBManager.getEm().getTransaction().commit();

            data = new Object[a.getSongList().size()][3];
            Iterator itr=a.getSongList().iterator();
            int tableCounter=0;
            this.songs.clear();//Παρκατω την ξαναγεμιζω με add(s1)
            while (itr.hasNext()){
                Song s1=(Song)itr.next();
                this.songs.add(s1);
                System.out.println(s1.getTitle());

                data[tableCounter][0]=s1.getTitle();
                data[tableCounter][1]=s1.getTracknr();
                data[tableCounter][2]=s1.getDuration();
                tableCounter++;
            }

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                data,
                new String [] {
                    "Τίτλος", "Αριθμός", "Διάρκεια",
                }
            ));
        }
        else{
            JOptionPane.showMessageDialog(null, "Δεν επιλέξατε κάποιο τραγούδι", "Λάθος", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jComboBox7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox7ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox7ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField5ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField5ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JComboBox jComboBox6;
    private javax.swing.JComboBox jComboBox7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
